<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Product;
use Template;
use Ad;
use Menu;
class ProductController extends Controller
{
    //
    public function insert(Request $request){
        try{
            Product::insert($request);
            $respond=array(
                'ret'=>200,
                'date'=>[
                    'status'=>1
                ]
            );
            return json_encode($respond);
        }
        catch(Exception $e){
            $respond=array(
                'ret'=>202,
                'date'=>[]
            );
            return json_encode($respond);
        }
    }

    public function getList(Request $request){
        try{
            if($request->has('uid')){
                $list=Product::getList($request->input('uid'),6);
            }
            else{
                $list=Product::getAllList(6);
            }


            $result=Template::get();
            $result['list']=$list;
            $ad=Ad::posGet('product');
            $result['menu']=Menu::getCateMenu(2);
            $parent=Menu::getParentTree($request->input('uid'),2);
            $result['parent']=$parent;
            $result['ad']=$ad;
            $respond=array(
                'ret'=>200,
                'date'=>$result
            );

            if($request->is('api/*')){
                return json_encode($respond);
            }
            else{

                return view('default.product')->with('result',$result)->with('page_name',$request->has('page_name')?$request->input('page_name'):'');
            }
        }
        catch(Exception $e){
            $respond=array(
                'ret'=>202,
                'date'=>[]
            );
            return json_encode($respond);
        }
    }

    public function detail(Request $request){
        $detail=Product::detail($request->input('uid'));
        $result=Template::get();
        $ad=Ad::posGet('product');
        $result['ad']=$ad;
        $result['detail']=$detail;
        $result['menu']=Menu::getCateMenu(2);
        $parent=Menu::getParentTree($detail[0]->belone_menu_uid,2);
        $result['parent']=$parent;
        $respond=array(
            'ret'=>200,
            'date'=>$result
        );


        if($request->is('api/*')){
            return json_encode($respond);
        }
        else{

            return view('default.product_detail')->with('result',$result)->with('page_name',$request->has('page_name')?$request->input('page_name'):'')->with('second','../');
        }
    }
}
